﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Corsat.Library;
using Corsat.Library.DataManager;
using Corsat.Library.Logic;
using Microsoft.Reporting.WebForms;

namespace Corsat.Web._2013.Reports
{
    public partial class ResultReport : System.Web.UI.Page
    {

        public List<CourseInReports> CourseInReportses
        {
            get
            {
                if (Session["CourseInReportses"] != null)
                {
                    return (List<CourseInReports>)Session["CourseInReportses"];
                }
                return new List<CourseInReports>();
            }
            set { Session["CourseInReportses"] = value; }
        }


        public List<Corsat.Library.Item> TechCourses
        {
            get
            {
                if (Session["TechCourses"] != null)
                {
                    return (List<Corsat.Library.Item>)Session["TechCourses"];
                }
                return new List<Corsat.Library.Item>();
            }
            set { Session["TechCourses"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CourseInReportses = Reporting.GetAllCoursesForReporting();
                ddlCourses.DataSource = CourseInReportses;
                ddlCourses.DataBind();
                ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
                divCourses.Visible = false;
                divTechCourses.Visible = false;
                divComitteeReport.Visible = false;
               TechCourses= Main.GetAllItemsByCategory(Category.TechnologyCourses);

            }
        }

        private void BindCourseReport(int courseId, string courseName)
        {
            reportViewer.LocalReport.DataSources.Clear();
            switch (reportViewer.LocalReport.ReportPath)
            {
                case "Reports\\SeatNumbers.rdlc":
                case "Reports\\SeatNumbers_ResultsReady.rdlc":
                case "Reports\\SeatNumbers_WithSignature.rdlc":
                    reportViewer.LocalReport.DataSources.Add(new ReportDataSource("Accounts", Reporting.GetAccounts(courseId)));
                    break;
                case "Reports\\Results.rdlc":
                    reportViewer.LocalReport.DataSources.Add(new ReportDataSource("Results", Reporting.GetResults(courseId, false)));
                    break;

                case "Reports\\ResultsWithPhones.rdlc":
                    reportViewer.LocalReport.DataSources.Add(new ReportDataSource("Results",
                                                                                  Reporting.GetResults(courseId, true)));
                    break;
                case "Reports\\AbsenceReport.rdlc":
                    DataTable absence = Main.GetAbsence(courseId, DateTime.Now.Year);
                    List<AbsenceReportView> resultList = new List<AbsenceReportView>();
                    if (absence != null)
                    {
                        reportViewer.LocalReport.DataSources.Clear();
                        reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetAbsence", absence));
                    }
                    else
                    {
                        reportViewer.LocalReport.DataSources.Clear();
                        reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetAbsence", resultList));
                    }
                    break;
            }
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.SetParameters(new ReportParameter("CourseName", courseName));
        }
        #region SeatsNumbers

        private void BindSeatNumberReport(int courseId, string courseName)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\SeatNumbers.rdlc";

            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource("Accounts", Reporting.GetAccounts(courseId)));
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.SetParameters(new ReportParameter("CourseName", courseName));
        }
        protected void Tokos1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(1, "الطقوس" + " - " + "المستوى الأول");
        }
        protected void Tokos2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(2, "الطقوس" + " - " + "المستوى الثاني");
        }

        protected void Abaayat_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(3, "الآبائيات");
        }

        protected void TanmayatMaharat_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(4, "تنمية المهارات");
        }

        protected void KatabMokadas1Old_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(5, "الكتاب المقدس" + " - " + "المستوى الأول - العهد القديم");
        }

        protected void KatabMokadas1New_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(6, "الكتاب المقدس" + " - " + "المستوى الأول - العهد الجديد");
        }

        protected void KatabMokadas2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(7, "الكتاب المقدس" + " - " + "المستوى الثاني");
        }

        protected void TarbiaWaAlmNafs1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(8, "التربية و علم النفس" + " - " + "المستوى الأول");
        }

        protected void TarbiaWaAlmNafs2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(9, "التربية و علم النفس" + " - " + "المستوى الثاني");
        }

        protected void MosharkaWatnaya_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(10, "المشاركة الوطنية");
        }

        protected void KaiadaWaAdara1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(11, "القيادة و الإدارة" + " - " + "المستوى الأول");
        }

        protected void KaiadaWaAdara2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(12, "القيادة و الإدارة" + " - " + "المستوى الثاني");
        }

        protected void Akida1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(13, "العقيدة" + " - " + "المستوى الأول");
        }

        protected void Akida2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(14, "العقيدة" + " - " + "المستوى الثاني");
        }

        protected void AbtyWaAl7an1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(15, "اللغة القبطية و الألحان" + " - " + "المستوى الأول");
        }

        protected void AbtyWaAl7an2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(16, "اللغة القبطية و الألحان" + " - " + "المستوى الثاني");
        }

        protected void TanmaiaAktasadia_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(17, "التنمية الإقتصادية");
        }

        protected void HaiahOsaraya1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(18, "الحياة الأسرية" + " - " + "المستوى الأول - شباب");
        }

        protected void HaiahOsaraya2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(19, "الحياة الأسرية" + " - " + "المستوى الثانى - مشورة");
        }

        protected void TarikhKnisa1_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(20, "تاريخ الكنيسة" + " - " + "المستوى الأول");
        }

        protected void TarikhKnisa2_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(21, "تاريخ الكنيسة" + " - " + "المستوى الثاني");
        }

        protected void TanmaiaSakafia_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(22, "التنمية الثقافية");
        }

        protected void MokafhatAdman_Click(object sender, EventArgs e)
        {
            BindSeatNumberReport(23, "التنمية الثقافية");
        }
        #endregion


        protected void DdlCourses_Changed(object sender, EventArgs e)
        {
            reportViewer.LocalReport.DataSources.Clear();
            int selectedId;
            if (int.TryParse(ddlCourses.SelectedValue, out selectedId) && selectedId > 0)
            {
                CourseInReports selectedCourse = CourseInReportses.FirstOrDefault(c => c.CourseId == (selectedId));
                ddlLevels.Items.Clear();
                ddlLevels.DataSource = selectedCourse.Levels;
                ddlLevels.DataBind();
            }
        }

        protected void BtnSearch_Clicked(object sender, EventArgs e)
        {
            int selectedId;
            int.TryParse(ddlLevels.SelectedValue, out selectedId);
            if (selectedId > 0)
            {
                BindCourseReport(selectedId, ddlCourses.SelectedItem.Text + " - " + ddlLevels.SelectedItem.Text);
            }
            else
            {
                int.TryParse(ddlCourses.SelectedValue, out selectedId);
                if (selectedId > 0)
                {
                    BindCourseReport(selectedId, ddlCourses.SelectedItem.Text);
                }
            }
        }

        protected void ArkamElGolos_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\SeatNumbers.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlCourses.DataSource = CourseInReportses;
            ddlCourses.DataBind();
            ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divCourses.Visible = true;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
        }


        protected void ArkamElGolosTawki3at_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\SeatNumbers_WithSignature.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlCourses.DataSource = CourseInReportses;
            ddlCourses.DataBind();
            ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divCourses.Visible = true;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
        }
        protected void Natayg_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\Results.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlCourses.DataSource = CourseInReportses;
            ddlCourses.DataBind();
            ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divCourses.Visible = true;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
        }
        protected void NataygWithMobile_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\ResultsWithPhones.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlCourses.DataSource = CourseInReportses;
            ddlCourses.DataBind();
            ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divCourses.Visible = true;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
        }
        protected void NataygTech_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\TechnologyResultsReport.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlTechCourse.DataSource = TechCourses;
            ddlTechCourse.DataBind();
            ddlTechCourse.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divTechCourses.Visible = true;
            divCourses.Visible = false;
            divComitteeReport.Visible = false;
        }

        protected void BtnSearchTech_Clicked(object sender, EventArgs e)
        {
            reportViewer.Reset();
            reportViewer.LocalReport.ReportPath = "Reports\\TechnologyResultsReport.rdlc";
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.DataSources.Clear();
            int courseId = 0;
            string courseTitleName = "";
            if (ddlTechCourse.SelectedItem != null)
            {
                courseId = int.Parse(ddlTechCourse.SelectedValue);
                courseTitleName = ddlTechCourse.SelectedItem.Text;
            }
            if (courseId > 0)
            {
                reportViewer.LocalReport.Refresh();
                reportViewer.LocalReport.SetParameters(new ReportParameter("ReportName", courseTitleName));
                DataTable absence;
                if (chkByDate.Checked)
                {
                    absence = Main.GetTechnologyResults(courseId, DateTime.Now.Year, DateTime.Parse(ddlExamDates.SelectedValue));
                    reportViewer.LocalReport.SetParameters(new ReportParameter("ReportDate",
                                                                               string.Format(
                                                                                   ddlExamDates.SelectedItem.Text,
                                                                                   "dd/MM/yyyy")));
                }
                else
                {
                    absence =Main.GetTechnologyResults(courseId, DateTime.Now.Year,null);
                    reportViewer.LocalReport.SetParameters(new ReportParameter("ReportDate", ""));
                }
                List<AbsenceReportView> resultList = new List<AbsenceReportView>();
                if (absence != null)
                {
                    reportViewer.LocalReport.DataSources.Clear();
                    reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetTechnologyResults", absence));
                }
                else
                {
                    reportViewer.LocalReport.DataSources.Clear();
                    reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetTechnologyResults", resultList));
                }
            }
        }



        protected void BtnCommittee_Clicked(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\ConferenceReport.rdlc";
            reportViewer.LocalReport.Refresh();
            divTechCourses.Visible = false;
            divCourses.Visible = false;
            divComitteeReport.Visible = true;
        }
        protected void BtnSearchCommittee_Clicked(object sender, EventArgs e)
        {
            reportViewer.LocalReport.DataSources.Clear();

            int status = 0;
            if (rbtBooked.Checked)
            {
                status = (int)ConferenceStatus.Booked;
            }
            else
                if (rbtCompleteBook.Checked)
                {
                    status = (int)ConferenceStatus.CompeleteBooking;
                }
            DataTable absence;
            if (ChkDateComittee.Checked)
            {
                absence = Main.GetConferenceData(status, DateTime.Now.Year, calendarComittee.SelectedDate);
            }
            else
            {
                absence = Main.GetConferenceData(status, DateTime.Now.Year, null);
            }


            if (absence != null)
            {
                reportViewer.LocalReport.DataSources.Clear();
                reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetConference", absence));
            }
            else
            {
                reportViewer.LocalReport.DataSources.Clear();
                reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetConference", new List<Account>()));
            }
            reportViewer.LocalReport.Refresh();
        }
        protected void Ad5alElBayanat_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\SeatNumbers_ResultsReady.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlCourses.DataSource = CourseInReportses;
            ddlCourses.DataBind();
            ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divCourses.Visible = true;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
        }

        protected void Archieve_Click(object sender, EventArgs e)
        {
            divCourses.Visible = false;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
            reportViewer.LocalReport.ReportPath = "Reports\\Archive.rdlc";
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource("Archive", Reporting.GetArchiveReport()));

            reportViewer.LocalReport.Refresh();

            reportViewer.LocalReport.SetParameters(new ReportParameter("Year", DateTime.Now.Year.ToString()));
        }

        protected void Shahadat_Click(object sender, EventArgs e)
        {
            divCourses.Visible = false;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
            reportViewer.LocalReport.ReportPath = "Reports\\CertificatesReport.rdlc";
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", Reporting.GetAllCertificates()));

            reportViewer.LocalReport.Refresh();
        }

        protected void Gholaf_Click(object sender, EventArgs e)
        {
            divCourses.Visible = false;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
            reportViewer.LocalReport.ReportPath = "Reports\\CertificateEnvelopesReport.rdlc";
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet", Reporting.GetAllCertificates()));

            reportViewer.LocalReport.Refresh();
        }

        protected void Ghyab_Click(object sender, EventArgs e)
        {
            reportViewer.LocalReport.ReportPath = "Reports\\AbsenceReport.rdlc";
            reportViewer.LocalReport.Refresh();
            ddlCourses.DataSource = CourseInReportses;
            ddlCourses.DataBind();
            ddlCourses.Items.Insert(0, new ListItem { Text = "أختر كورس", Value = "0" });
            divCourses.Visible = true;
            divTechCourses.Visible = false;
            divComitteeReport.Visible = false;
        }

        protected void ChkDate_CheckedChanged(object sender, EventArgs e)
        {
            reportViewer.LocalReport.DataSources.Clear();
            ddlExamDates.Enabled = chkByDate.Checked;
        }

        protected void ChkDateComittee_CheckedChanged(object sender, EventArgs e)
        {
            reportViewer.LocalReport.DataSources.Clear();
            calendarComittee.Enabled = ChkDateComittee.Checked;
            if (ChkDateComittee.Checked)
            {
                calendarComittee.SelectedDate = DateTime.Now.Date;
            }
        }

        protected void ddlCourseNames_SelectedIndexChanged(object sender, EventArgs e)
        {

            reportViewer.LocalReport.DataSources.Clear();
            ddlExamDates.Items.Clear();
            if (string.IsNullOrWhiteSpace(ddlTechCourse.SelectedValue) || Convert.ToInt32(ddlTechCourse.SelectedValue) == 0)
            {
                return;
            }
            /*int techCourseId = Convert.ToInt32(ddlTechCourse.SelectedValue);
            if (techCourseId == (int)TechnologyCourse.Introduction || techCourseId == (int)TechnologyCourse.WindowsAndVideoStudio || techCourseId == (int)TechnologyCourse.Photoshop)
            {
                ddlExamDates.Items.Add(new ListItem(" الجمعة - " + "25 يوليو", "7/25/2014"));
                ddlExamDates.Items.Add(new ListItem(" السبت - " + "26 يوليو", "7/26/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأحد - " + "27 يوليو", "7/27/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأثنين - " + "28 يوليو", "7/28/2014"));
                ddlExamDates.Items.Add(new ListItem(" الثلاثاء - " + "29 يوليو", "7/29/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأربعاء - " + "30 يوليو", "7/30/2014"));
                ddlExamDates.Items.Add(new ListItem(" الجمعة - " + "1 أغسطس", "8/1/2014"));
            }
            else
            {
                ddlExamDates.Items.Add(new ListItem(" السبت - " + "2 أغسطس", "8/2/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأحد - " + "3 أغسطس", "8/3/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأثنين - " + "4 أغسطس", "8/4/2014"));
                ddlExamDates.Items.Add(new ListItem(" الثلاثاء - " + "5 أغسطس", "8/5/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأربعاء - " + "6 أغسطس", "8/6/2014"));
                ddlExamDates.Items.Add(new ListItem(" الخميس - " + "7 أغسطس", "8/7/2014"));
                ddlExamDates.Items.Add(new ListItem(" الجمعة - " + "8 أغسطس", "8/8/2014"));
            }*/
                ddlExamDates.Items.Add(new ListItem(" الجمعة - " + "25 يوليو", "7/25/2014"));
                ddlExamDates.Items.Add(new ListItem(" السبت - " + "26 يوليو", "7/26/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأحد - " + "27 يوليو", "7/27/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأثنين - " + "28 يوليو", "7/28/2014"));
                ddlExamDates.Items.Add(new ListItem(" الثلاثاء - " + "29 يوليو", "7/29/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأربعاء - " + "30 يوليو", "7/30/2014"));
                ddlExamDates.Items.Add(new ListItem(" الجمعة - " + "1 أغسطس", "8/1/2014"));
            
                ddlExamDates.Items.Add(new ListItem(" السبت - " + "2 أغسطس", "8/2/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأحد - " + "3 أغسطس", "8/3/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأثنين - " + "4 أغسطس", "8/4/2014"));
                ddlExamDates.Items.Add(new ListItem(" الثلاثاء - " + "5 أغسطس", "8/5/2014"));
                ddlExamDates.Items.Add(new ListItem(" الأربعاء - " + "6 أغسطس", "8/6/2014"));
                ddlExamDates.Items.Add(new ListItem(" الخميس - " + "7 أغسطس", "8/7/2014"));
                ddlExamDates.Items.Add(new ListItem(" الجمعة - " + "8 أغسطس", "8/8/2014"));
            
        }
    }
}